package com.cskaoyan.service.topic;

import com.cskaoyan.bean.MallGoods;
import com.cskaoyan.bean.MallTopic;
import com.cskaoyan.bean.MallTopicExample;
import com.cskaoyan.mapper.MallGoodsMapper;
import com.cskaoyan.mapper.MallTopicMapper;
import com.cskaoyan.model.vo.topic.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @program: project-market
 * @description:
 * @author: zhijie
 * @create: 2021-12-02 19:30
 **/
@Service
public class WxTopicServiceImpl implements WxTopicService {
    @Autowired
    MallTopicMapper mallTopicMapper;
    @Autowired
    MallGoodsMapper mallGoodsMapper;

    @Override
    public WxTopicListVo topicList(Integer page, Integer limit) {
        PageHelper.startPage(page, limit);
        List<WxTopicListData> listData = mallTopicMapper.selectTopicData();
        PageInfo<WxTopicListData> info = new PageInfo<>(listData);
        WxTopicListVo vo = new WxTopicListVo();
        vo.setLimit(limit);
        vo.setPage(page);
        vo.setList(listData);
        vo.setPages(info.getPages());
        vo.setTotal(info.getTotal());
        return vo;
    }

    @Override
    public WxTopicDetailVo topicDetail(Integer id) {
        MallTopic mallTopic = mallTopicMapper.selectByPrimaryKey(id);
        Integer[] goods = mallTopic.getGoods();
        WxTopicDetailVo vo = new WxTopicDetailVo();
        vo.setTopic(mallTopic);
        if (goods.length!=0) {
            List<WxTopicDetailData> topicGoods = mallGoodsMapper.selectTopicGoods(goods);
            vo.setGoods(topicGoods);
        }
        return vo;
    }

    @Override
    public WxTopicRelatedVo topicRelated(Integer id) {
        PageHelper.startPage(1,4);
        MallTopicExample example = new MallTopicExample();
        example.createCriteria().andIdGreaterThan(id);
        List<MallTopic> mallTopics = mallTopicMapper.selectByExample(example);
        PageInfo<MallTopic> info = new PageInfo<>(mallTopics);
        WxTopicRelatedVo vo = new WxTopicRelatedVo();
        vo.setLimit(4);
        vo.setPage(1);
        vo.setPages(info.getPages());
        vo.setTotal(info.getTotal());
        vo.setList(mallTopics);
        return vo;
    }
}
